#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

const string rootpath = "./root";//网页根目录
const string homepage = "index.html";//首页

class Request
{
public:
    //分割报文
    void deserialize(string req)
    {
        while (1)
        {
            //报头每行都是以\r\n分割的
            size_t pos = req.find("\r\n");
            if (pos == string::npos)
                break;
            string tem = req.substr(0, pos);
            if(tem.empty())//读到空行的时候就会跳出
                break;
            head_.push_back(tem);
            req.erase(0,pos+2);
        }
        text_ = req;

        Prase_first_head();
    }

    //分割请求行(第一行报头,以空格分割的)
    void Prase_first_head()
    {
        stringstream ss(head_[0]);
        ss >> method_ >> url_ >> http_version_;
        path_ = rootpath;
        if(url_ == "/")//访问根目录
        {
            path_ += "/";
            path_ +=homepage;
        }
        else
        {
            path_ += url_;
        }

    }

    vector<string> head_; // 请求的头部信息
    string text_;         // 请求正文

    //请求行属性
    string method_;
    string path_;
    string url_;
    string http_version_;
    string err_path_ = "./root/err.html";
};
